package com.ys.controller.api.hr;

import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ys.config.MenuId;
import com.ys.entity.hr.TwlStock;
import com.ys.entity.hr.TwlStockIn;
import com.ys.entity.sys.YsCol;
import com.ys.enume.sys.YsColEnum;
import com.ys.service.sys.CommonApiService;
import com.ys.service.sys.UploadService.UploadDto;
import com.ys.util.ExportExcelUtils;
import com.ys.vo.LayuiTableVoJA;
import com.ys.vo.R;

@Controller
@RequestMapping("/api/inter/twlStock")
public class ApiInterTwlStockController {
	
//	private static final Logger logger = Logger.getLogger(ApiAdminLoginController.class);
	
	@Autowired
	private CommonApiService<TwlStock> commonApiService;
	
	@RequiresPermissions("twlStock:view")
	@RequestMapping("/search")
    @ResponseBody
	public LayuiTableVoJA search(@ModelAttribute TwlStock cond){
    	
    	cond.addField("(SELECT ISNULL(SUM(AA.Num), 0) FROM TWL_StockBorrowDetail AA LEFT JOIN TWL_StockBorrow BB ON BB.Bid = AA.BidStockBorrow WHERE BB.EffectStock = 1 AND AA.StockId = A.Id) numBorrowed");
		cond = setSeachSql(cond);
		cond.setOrderby("A.Id DESC");
		return commonApiService.searchLay(cond);
	}
	
	@RequestMapping("/searchForModal")
    @ResponseBody
	public LayuiTableVoJA searchForModal(@ModelAttribute TwlStock cond){
		
		cond
			.setStatus(1)
			.setOrderby("A.Id ASC")
			.addField("DATEDIFF(DAY, StockInDay , getdate()) stockDays");
		cond.addLeftJoin("TB_Material B ON B.Code = A.MaterialCode")
			.addField("B.Name materialName")
			.addField("B.Remark materialRemark");
		
		cond.addLeftJoin("TWL_StockInDetail C ON C.Id = A.StockInDetailId")
			.addField("C.BidStockIn bidStockIn")
			.addField("C.Img stockInImg")
			.addField("C.Price stockInPrice");
		
		cond.addLeftJoin("TB_CompanyInfo D ON D.Vid = A.CompanyVid")
			.addField("D.Vname companyVname");
		
		cond.addLeftJoin("TS_OrganizationInfo E ON E.Orgid = A.Orgid")
			.addField("E.OrgName orgName")
			.addField("E.HeadUserId orgHeadUserId");
		return commonApiService.searchLay(cond);
	}
	
	/*
	 * 导出
	 */
	@RequiresPermissions("twlStock:export")
	@RequestMapping("/export")
    @ResponseBody
    public R export(@ModelAttribute TwlStock cond) throws Exception{
		cond = setSeachSql(cond);
		cond.setOrderby("A.Id DESC");
		
		cond.setPage(0).setSize(999999);

		List<Map<String, Object>> listData = cond.queryMap();
        
    	List<YsCol> listField = new YsCol()
    			.setMenuId(MenuId.TB_Material)
    			.setFrontShow(YsColEnum.FrontShow.SHOW.value)
    			.setOrderby("A.SortNo asc")
    			.queryList();
    	
    	
        ExportExcelUtils<TwlStockIn> util = new ExportExcelUtils<TwlStockIn>(new TwlStockIn());
        
        String toFilePath = util.exportExcel(listData, listField, "物料名称维护");
        UploadDto dto = commonApiService.uploadLocalToServer(toFilePath);
        return R.ok(dto.getUrl());
    }

    /**
     * 设置查询的cond的sql设置
     * @param cond
     * @return
     */
    public TwlStock setSeachSql(TwlStock cond){
    	
    	cond.addLeftJoin("TB_Material E ON E.Code = A.MaterialCode")
    		.addField("E.Name materialName")
    		.addField("E.Remark materialRemark");
    	
    	cond.addLeftJoin("TWL_StockInDetail B ON B.Id = A.StockInDetailId")
    		.addField("B.BidStockIn bidStockIn")
    		.addField("B.Price price")
    		.addField("B.Img img");
    	
    	cond.addLeftJoin("TB_CompanyInfo C on C.Vid = A.CompanyVid")
			.addField("C.Vid companyVid")
    		.addField("C.Vname companyVname");
    	
    	cond.addLeftJoin("TS_OrganizationInfo D on D.Orgid = A.Orgid")
			.addField("D.Orgid orgid")
    		.addField("D.OrgName orgName")
    		.addField("D.HeadUserId orgHeadUserId");
		
        return  cond;
    }
	@RequestMapping("/searchForBorrow")
	@ResponseBody
	public LayuiTableVoJA searchForBorrow(@ModelAttribute TwlStock cond){

		cond
			.addField("(SELECT ISNULL(SUM(AA.Num), 0) FROM TWL_StockBorrowDetail AA LEFT JOIN TWL_StockBorrow BB ON BB.Bid = AA.BidStockBorrow WHERE BB.EffectStock = 1 AND AA.StockId = A.Id) numBorrowed")
			.setStatus(1)
			.setOrderby("A.Id DESC")
			.addField("DATEDIFF(DAY, StockInDay , getdate()) stockInDays");

		cond.addLeftJoin("TB_Material B ON B.Code = A.MaterialCode")
			.addField("B.Name materialName")
			.addField("B.Remark materialRemark");

		cond.addLeftJoin("TWL_StockInDetail C ON C.Id = A.StockInDetailId")
			.addField("C.BidStockIn bidStockIn")
			.addField("C.Img img")
			.addField("C.Price price")
			.addField("C.Remark stockRemark");

		cond.addLeftJoin("TB_CompanyInfo D ON D.Vid = A.CompanyVid")
			.addField("D.Vname companyVname");
		return commonApiService.searchLay(cond);
	}
}
